package cn.liyunqi.ttv.start.controller;

import java.net.URL;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.StringUtil;
import cn.liyunqi.ttv.start.action.TTVActionUtil;
import cn.liyunqi.ttv.start.localdb.ConfigHelper;
import cn.liyunqi.ttv.start.util.UrlUtil;
import cn.liyunqi.ttv.start.view.VersionInfo;
import cn.liyunqi.ttv.start.view.VersionWork;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;

import java.util.ResourceBundle;


public class InitAppController extends BaseFXController {

    @FXML
    private TextArea txtInitValue;

    @FXML
    private Button loginBtn;

    @FXML
    private Button registerBtn;


    public void initialize(URL location, ResourceBundle resources) {

        txtInitValue.setDisable(true);
        loginBtn.setDisable(true);
        registerBtn.setDisable(true);

        //开启线程启动滚动条
        Task copyWorker = createInitWorker();

        copyWorker.messageProperty().addListener(new ChangeListener<String>() {
            public void changed(ObservableValue<? extends String> observable, String oldValue,
                                String newValue) {
                System.out.println(newValue);
            }
        });
        new Thread(copyWorker).start();

    }

    public Task createInitWorker() {

        return new Task() {

            @Override
            protected Object call() throws Exception {

                //1获取版本信息
                //1.1
                //获取当前版本
                try {
                    txtInitValue.setText("正在检测更新");
                    VersionInfo version = TTVActionUtil.getNewVersion();
                    if (version == null || StringUtil.isEmptyOrNull(version.getVersionValue())) {
                        txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "获取最新版本号异常.....");
                        loginBtn.setDisable(false);
                        registerBtn.setDisable(false);
                    } else {
                        String localversion = ConfigHelper.getloadValue("localversion");
                        if (StringUtil.isEmptyOrNull(localversion)) {
                            txtInitValue.setText("当前版本号获取异常正在下载全新版本");
                            downLoadNewVersion(txtInitValue, version);
                            return true;
                        }
                        txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "当前版本号.....v" + localversion);
                        txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "获取最新版本号.....v" + version.getVersionValue());
                        if (!localversion.equals(version.getVersionValue())) {
                            //下载最新代码
                            downLoadNewVersion(txtInitValue, version);
                        } else {
                            //正常登录
                            loginBtn.setDisable(false);
                            registerBtn.setDisable(false);
                        }
                    }
                } catch (BizException biz) {
                    txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + biz.getMessage());
                }
                return true;
            }

        };
    }
    private void downLoadNewVersion(TextArea txtInitValue,VersionInfo version){
        if(1 == version.getIsAll()){
            txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + version.getMessage());
            return;
        }else{
            if(version.getWorkList() != null){
                for (int i = 0; i < version.getWorkList().size(); i++) {
                    VersionWork vo = version.getWorkList().get(i);
                    if(1 == vo.getWorkType()){
                        try {
                            txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "正在安装资源文件");
                            ConfigHelper.execute(vo.getSqlValue());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else {
                        txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "正在下载资源文件...");
                        TTVActionUtil.getResources(vo.getUrl(), vo.getFile());
                        txtInitValue.setText(txtInitValue.getText() + UrlUtil.R + "下载资源文件完成...");
                    }
                }
            }
            loginBtn.setDisable(false);
            registerBtn.setDisable(false);
        }
    }

    @FXML
    public void loginHandle(){
        LoginController controller = (LoginController) loadFXMLPage("登录", FXMLPage.LoginUI, false);
        controller.setInitUIController(this);
        controller.showDialogStage();
    }

    @FXML
    public void registerHandle(){
        RegisterController controller = (RegisterController) loadFXMLPage("注册", FXMLPage.RegisterUI, false);
        controller.setInitUIController(this);
        controller.showDialogStage();
    }


}
